<?php

namespace app\model;

use ReflectionClass;
use think\facade\Db;
use think\facade\Log;

function findTableNameRef($clazz)
{
    $refPropety = null;
    $ref = new ReflectionClass($clazz);
    $proppertiesArray = $ref->getDefaultProperties();
    //反射不到东西 就直接使用clazz作为tablename
    return $proppertiesArray['table'] ?? $clazz;
}

class BaseQuery
{

    var $tableName;

    /**
     * @param $tableName
     */
    public function __construct($clazz, $isTaleName = false)
    {
        if ($isTaleName) {
            $this->tableName = $clazz;
        } else {
            $_tableName = findTableNameRef($clazz);
            Log::info("初始化query  tablename:{tablename}", [
                "tablename" => $_tableName,
            ]);
            $this->tableName = $_tableName;
        }

    }

    public function getQuery()
    {
        return Db::table($this->tableName);
    }

    public static function build($tableName, $isTaleName = false)
    {
        $query = new BaseQuery($tableName, $isTaleName);
        /*查询非软的*/
        return $query->getQuery()->where('delete_time', 'null');
    }


    public static function raw(string $sql, $params)
    {
        return Db::query($sql, $params);
    }


}